import { initField, setField, typeOf, debounce } from "../utils/tool.js"
import moment from 'moment'

let Input = (params) => {
    let value = params.fieldModel[params.field.key]
    let debounceTime = params.debounceTime ? params.debounceTime : 0
    return {
        attrs: {
            clearable: true
        },
        props: {
            value: value
        },
        on: {
            input: debounce(value => {
                setField(params.fieldModel, params.field.models, value)
            }, debounceTime)
        }
    }
}

let InputNumber = (params) => {
    let value = params.fieldModel[params.field.key]
    let debounceTime = params.debounceTime ? params.debounceTime : 0
    return {
        props: {
            value: value
        },
        style:{
            width: "100%"
        },
        on: {
            input: debounce(value => {
                setField(params.fieldModel, params.field.models, value)
            }, debounceTime)
        }
    }
}

let SelectData = (params) => {
    let value = params.fieldModel[params.field.key]
    return {
        attrs: {
            clearable: true,
            "label-in-value": true
        },
        props: {
            value: value,
            data: params.field.data
        },
        on: {
            "on-change": function (labelInValue) {
                setField(params.fieldModel, params.field.models, labelInValue)
            }
        }
    }
}

let Coding = (params) => {
    let attrs = {
        multiple: false,
        clearable: true,
        "label-in-value": true,
        filterable: true
    }
    Object.assign(attrs, params.field.attrs)

    let value = params.fieldModel[params.field.key]

    if (attrs.multiple) {
        if (!value || value == "") {
            value = []
        } else {
            value = value.split(",").map(val => parseInt(val))
        }
    }
    return {
        attrs: attrs,
        props: {
            value: value
        },
        on: {
            "on-change": function (value) {
                let v = value

                if (attrs.multiple) {
                    v = {}
                    if (value.length > 0) {
                        let keys = Object.keys(value[0])
                        keys.map(key => {
                            v[key] = value.map(val => val[key]).join(",")
                        })
                    }
                }

                setField(params.fieldModel, params.field.models, v)

                if (params.field.on && params.field.on["on-option-change"]) params.field.on["on-option-change"](value, params.field, params.fieldModel)
            }
        }
    }
}

let DatePicker = (params) => {
    let value = params.fieldModel[params.field.key]
    if (typeOf(value) == "number"){
        value = moment(value).format('YYYY-MM-DD')
    }
    return {
        attrs: {
            format: "yyyy-MM-dd"
        },
        props: {
            value: value
        },
        on: {
            "on-change": function (value) {
                setField(params.fieldModel, params.field.models, value)
            }
        },
    }
}

let Area = (params) => {
    let value = params.fieldModel[params.field.key]
    if (value || value != "") value = parseInt(value)
    return {
        attrs: {
            filterable: true,
            clearable: true,
        },
        props: {
            value: value
        },
        on: {
            "on-change": function (value, selectedData) {
                setField(params.fieldModel, params.field.models, selectedData[value.length - 1])
            }
        }
    }
}

let AreaAsyn = (params) => {
    let value = params.fieldModel[params.field.key]
    if (value || value != "") value = parseInt(value)
    return {
        attrs: {
            filterable: true,
            clearable: true,
        },
        props: {
            value: value
        },
        on: {
            "on-change": function (value, selectedData) {
                setField(params.fieldModel, params.field.models, selectedData[value.length - 1])
            }
        }
    }
}

let Org = (params) => {
    let value = params.fieldModel[params.field.key]
    if (value || value != "") value = parseInt(value)
    return {
        attrs: {
            filterable: true
        },
        props: {
            value: value
        },
        on: {
            "on-change": function (value, selectedData) {
                setField(params.fieldModel, params.field.models, selectedData[value.length - 1])
            }
        }
    }
}

let Building = (params) => {
    let value = params.fieldModel["params.field.key"]
    return {
        attrs: {
            filterable: true
        },
        props: {
            orgid: value,
            filterable: true
        },
        on: {
            "on-change": function (value) {
                setField(params.fieldModel, params.field.models, value)
            }
        }
    }
}

let House = (params) => {
    let value = params.fieldModel["buildingid"]
    return {
        attrs: {
            filterable: true
        },
        props: {
            buildingid: value
        },
        on: {
            "on-change": function (value, selectedData) {
                setField(params.fieldModel, params.field.models, selectedData[value.length - 1])
            }
        }
    }
}

let Position = (params) => {
    let value = params.fieldModel[params.field.key]
    return {
        attrs: {
            filterable: true
        },
        props: {
            value: value
        },
        on: {
            "on-change": function (value, selectedData) {
                setField(params.fieldModel, params.field.models, selectedData[value.length - 1])
            }
        }
    }
}

let Role = (params) => {
    let attrs = {
        clearable: true,
        filterable: true,
        "label-in-value": true
    }
    Object.assign(attrs, params.field.attrs)

    let value = params.fieldModel[params.field.key]
    
    if (attrs.multiple) {
        if (value == "" || value == undefined) {
            value = []
        } else {
            value = value.split(",").map(val => parseInt(val))
        }
    }
    
    return {
        attrs: attrs,
        props: {
            value: value
        },
        on: {
            "on-change": function (labelInValue) {
                let value = labelInValue

                if (attrs.multiple) {
                    value = {}
                    if (labelInValue.length > 0) {
                        let keys = Object.keys(labelInValue[0])
                        keys.map(key => {
                            value[key] = labelInValue.map(val => val[key]).join(",")
                        })
                    }
                }
                
                setField(params.fieldModel, params.field.models, value)
            }
        }
    }
}

let Editor = (params) => {
    let value = params.fieldModel[params.field.key]
    return {
        props: {
            content: value
        },
        on: {
            "change": function (value) {
                setField(params.fieldModel, params.field.models, value.html)
            },
        }
    }
}

let User = (params) => {
    let attrs = {
        multiple: false
    }
    Object.assign(attrs, params.field.attrs)

    let value = params.fieldModel[params.field.key]

    if (attrs.multiple) {
        if (value == "") {
            value = []
        } else {
            value = value.split(",").map(val => parseInt(val))
        }
    }

    return {
        attrs: attrs,
        props: {
            value: value
        },
        on: {
            "on-option-select": function (value) {
                let v = value

                if (attrs.multiple) {
                    v = {}
                    if (value.length > 0) {
                        let keys = Object.keys(value[0])
                        keys.map(key => {
                            v[key] = value.map(val => val[key]).join(",")
                        })
                    }
                }

                setField(params.fieldModel, params.field.models, v)
            },
            "on-clear"() {
                initField(params.fieldModel, params.field.models)
            }
        }
    }
}

let option = (type, params) => {
    switch (type) {
        case "Input":
            return Input(params)
            break;

        case "InputNumber":
            return InputNumber(params)
            break;

        case "SelectData":
            return SelectData(params)
            break;

        case "Coding":
            return Coding(params)
            break;

        case "DatePicker":
            return DatePicker(params)
            break;

        case "Area":
            return Area(params)
            break;
        
        case "AreaAsyn":
            return AreaAsyn(params)
            break;

        case "Org":
            return Org(params)
            break;

        case "Building":
            return Building(params)
            break;

        case "House":
            return House(params)
            break;

        case "Position":
            return Position(params)
            break;

        case "Role":
            return Role(params)
            break;

        case "Editor":
            return Editor(params)
            break;

        case "User":
            return User(params)
            break;

        default:
            break;
    }
}

export default option